package tables

import (
	"fmt"
	"peilian-api/app/global/variable"
)

var (
	MsgTradeCreate  = "trade_TradeCreate"  // 交易创建
	MsgTradePaid    = "trade_TradePaid"    // 交易支付
	MsgTradeSuccess = "trade_TradeSuccess" // 交易成功
	MsgTradeClose   = "trade_TradeClose"   // 交易关闭

	TradeWaitBuyerPay        = "WAIT_BUYER_PAY"         // 等待买家付款
	TradePaid                = "TRADE_PAID"             // 订单已经支付成功
	TradeWaitConfirm         = "WAIT_CONFIRM"           // 待确认，买家已经付款，等待成团接单
	TradeWaitSellerSendGoods = "WAIT_SELLER_SEND_GOODS" // 等待买家确认收货
	TradeSuccess             = "TRADE_SUCCESS"          // 买家已经签单
	TradeClose               = "TRADE_CLOSED"           // 交易关闭
)

type YzTrade struct { //开始结束视频
	Model
	Bid                string `json:"bid" gorm:"type:varchar(512);not null;default:\"\";comment:交易id"`
	YzOpenId           string `json:"yz_open_id" gorm:"type:varchar(64);not null;default:\"\";comment:有赞用户id"`
	BuyerPhone         string `json:"buyer_phone" gorm:"type:varchar(32);not null;default:\"\";comment:买家手机号"`
	BuyerRealPhone     string `json:"buyer_real_phone" gorm:"type:varchar(11);not null;default:\"\";comment:买家真实手机号"`
	OuterUserId        string `json:"outer_user_id" gorm:"type:varchar(64);not null;default:\"\";comment:微信weixin_openid"`
	Status             string `json:"status" gorm:"type:varchar(64);not null;default:\"\";comment:订单状态"`
	CreateTradeTime    string `json:"create_trade_time" gorm:"type:varchar(64);not null;default:\"\";comment:创建订单"`
	CloseTradeTime     string `json:"close_trade_time" gorm:"type:varchar(64);not null;default:\"\";comment:订单关闭时间"`
	PayFinishTradeTime string `json:"pay_finish_trade_time" gorm:"type:varchar(64);not null;default:\"\";comment:订单支付完成时间"`
	ItemIds            string `json:"item_ids" gorm:"type:varchar(64);not null;default:\"\";comment:该交易所有的商品ids"`
	YsUserId           uint64 `json:"ys_user_id" gorm:"type:bigint(20);not null;default:0;comment:有时用户user_id"`
	MsgId              string `json:"msg_id" gorm:"type:varchar(64);not null;default:\"\";comment:消息唯一标识"`
	CloseReason        string `json:"close_reason" gorm:"type:varchar(256);not null;default:\"\";comment:关闭原因"`
	CloseType          int    `json:"close_type" gorm:"type:tinyint(4);not null;default:0;comment:关闭类型0:未关闭1:过期关闭"`
}

func (o *YzTrade) TableName() string {
	return "yz_trade"
}

// 创建交易
func (*YzTrade) CreateTrade(yzTrade YzTrade) error {
	d := variable.DB.MySQL.Create(&yzTrade)
	if d.RowsAffected == 0 {
		return fmt.Errorf("交易创建error")
	}
	return nil
}

// 更新交易信息 sel 要更新的字段
func (a *YzTrade) UpdateTrade(id uint, yzTrade YzTrade, sel ...string) error {
	if len(sel) == 0 {
		d := variable.DB.MySQL.Model(a).Where("id = ?", id).Updates(&yzTrade)
		if d.RowsAffected == 0 {
			return d.Error
		}
	} else {
		d := variable.DB.MySQL.Model(a).Select(sel).Where("id = ?", id).Updates(&yzTrade)
		if d.RowsAffected == 0 {
			return d.Error
		}
	}
	return nil
}

// 根据交易单号id查询交易信息
func (*YzTrade) GetTradeInfoByBid(bid string) (yzTrade YzTrade, exists bool, err error) {
	d := variable.DB.MySQL.Where("bid = ?", bid).Find(&yzTrade)
	var flag bool = true
	if d.RowsAffected == 0 {
		flag = false
	}
	return yzTrade, flag, d.Error
}
